package com.dl698.example;

import com.dl698.DL698Library;
import com.dl698.bean.FrameData;
import com.dl698.buss.Option;
import com.dl698.buss.LinkOption;
import com.dl698.buss.RequestOption;
import com.dl698.asn1.axdr.AxdrInteger;
import com.dl698.asn1.axdr.AxdrBoolean;
import com.dl698.asn1.axdr.AxdrOctetString;
import com.dl698.util.ReverseByteArrayOutputStream;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.dl698.util.DataUtils;

/**
 * DL698库使用示例
 */
public class DL698Example {

    public static void main(String[] args) {
        System.out.println("DL698 Java库使用示例");
        System.out.println("========================");

        // 获取DL698库实例
        DL698Library dl698 = DL698Library.getInstance();

        // // 示例1: 构建GET请求
        // System.out.println("示例1: 构建GET请求");
        // buildGetRequestExample(dl698);

        // // 示例2: 构建SET请求
        // System.out.println("\n示例2: 构建SET请求");
        // buildSetRequestExample(dl698);

        // // 示例3: 使用ASN.1类型
        // System.out.println("\n示例3: 使用ASN.1类型");
        // asn1TypeExample();

        // // 示例4: 数据工具使用
        // System.out.println("\n示例4: 数据工具使用");
        // dataUtilsExample();

        String frame = "68 47 00 c3 05 11 00 00 00 00 00 a0 f3 8e 85 01 00 45 00 05 00 01 02 06 0a 04 51 75 65 63 0a 04 30 31 30 38 0a 06 32 37 30 39 32 31 0a 04 52 30 30 31 0a 06 33 31 30 37 32 31 0a 08 45 43 32 30 30 41 43 4e 00 00 c9 31 16";
        frame = frame.replaceAll("\\s", "");
        FrameData data = dl698.parseFrame(DataUtils.hexToBytes(frame));
        System.out.println(data.toJsonString());
    }

    /**
     * 构建GET请求示例
     */
    private static void buildGetRequestExample(DL698Library dl698) {
        String serverAddress = "123456789012"; // 12字节服务器地址
        String clientAddress = "01"; // 2字节客户端地址

        // 创建简单的请求数据（这里用字符串表示）
        String requestData = "GET_REQUEST_DATA";

        // 构建GET请求帧
        FrameData result = dl698.buildGetRequest(serverAddress, clientAddress, requestData);

        System.out.println("GET请求构建结果:");
        System.out.println("  结果码: " + result.getCode());
        System.out.println("  数据: " + result.getData());
    }

    /**
     * 构建SET请求示例
     */
    private static void buildSetRequestExample(DL698Library dl698) {
        String serverAddress = "123456789012"; // 12字节服务器地址
        String clientAddress = "01"; // 2字节客户端地址

        // 创建设置数据（这里用字符串表示）
        String setData = "SET_REQUEST_DATA";

        // 构建SET请求帧
        FrameData result = dl698.buildSetRequest(serverAddress, clientAddress, setData);

        System.out.println("SET请求构建结果:");
        System.out.println("  结果码: " + result.getCode());
        System.out.println("  数据: " + result.getData());
    }

    /**
     * ASN.1类型使用示例
     */
    private static void asn1TypeExample() {
        try {
            // AxdrInteger示例
            AxdrInteger integer = new AxdrInteger();
            integer.setVal(12345L);

            ReverseByteArrayOutputStream outputStream = new ReverseByteArrayOutputStream();
            outputStream.setBufSize(10);
            int length = integer.encode(outputStream);
            byte[] encoded = outputStream.getArray();

            System.out.println("AxdrInteger编码:");
            System.out.println("  值: " + integer.getValue());
            System.out.println("  编码长度: " + length);
            System.out.println("  编码数据: " + DataUtils.bytesToHexString(encoded, 0, encoded.length));

            // AxdrBoolean示例
            AxdrBoolean bool = new AxdrBoolean(true);

            ReverseByteArrayOutputStream boolOutputStream = new ReverseByteArrayOutputStream();
            boolOutputStream.setBufSize(10);
            int boolLength = bool.encode(boolOutputStream);
            byte[] boolEncoded = boolOutputStream.getArray();

            System.out.println("\nAxdrBoolean编码:");
            System.out.println("  值: " + bool.getVal());
            System.out.println("  编码长度: " + boolLength);
            System.out.println("  编码数据: " + DataUtils.bytesToHexString(boolEncoded, 0, boolEncoded.length));

            // AxdrOctetString示例
            byte[] octetData = { 0x01, 0x02, 0x03, 0x04, 0x05 };
            AxdrOctetString octetString = new AxdrOctetString(octetData);

            ReverseByteArrayOutputStream octetOutputStream = new ReverseByteArrayOutputStream();
            octetOutputStream.setBufSize(10);
            int octetLength = octetString.encode(octetOutputStream);
            byte[] octetEncoded = octetOutputStream.getArray();

            System.out.println("\nAxdrOctetString编码:");
            System.out.println("  值: " + octetString.toString());
            System.out.println("  编码长度: " + octetLength);
            System.out.println("  编码数据: " + DataUtils.bytesToHexString(octetEncoded, 0, octetEncoded.length));

        } catch (Exception e) {
            System.err.println("ASN.1类型示例出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 数据工具使用示例
     */
    private static void dataUtilsExample() {
        try {
            // 十六进制字符串转字节数组
            String hexString = "0102030405060708";
            byte[] bytes = DataUtils.toBytes(hexString);
            System.out.println("十六进制转字节数组:");
            System.out.println("  输入: " + hexString);
            System.out.println("  输出: " + DataUtils.bytesToHexString(bytes, 0, bytes.length));

            // 整数转十六进制字符串
            int value = 255;
            String hex = DataUtils.intToHexString(value, 1);
            System.out.println("\n整数转十六进制:");
            System.out.println("  输入: " + value);
            System.out.println("  输出: " + hex);

            // 字节转十六进制字符串
            byte byteValue = (byte) 0xAB;
            String byteHex = DataUtils.byteToHexString(byteValue);
            System.out.println("\n字节转十六进制:");
            System.out.println("  输入: " + String.format("0x%02X", byteValue));
            System.out.println("  输出: " + byteHex);

        } catch (Exception e) {
            System.err.println("数据工具示例出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}